#
#  Externals Makefile.
#
#  Copyright (c) 2008-2019, Rice University.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are
#  met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  * Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
#  * Neither the name of Rice University (RICE) nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
#  This software is provided by RICE and contributors "as is" and any
#  express or implied warranties, including, but not limited to, the
#  implied warranties of merchantability and fitness for a particular
#  purpose are disclaimed. In no event shall RICE or contributors be
#  liable for any direct, indirect, incidental, special, exemplary, or
#  consequential damages (including, but not limited to, procurement of
#  substitute goods or services; loss of use, data, or profits; or
#  business interruption) however caused and on any theory of liability,
#  whether in contract, strict liability, or tort (including negligence
#  or otherwise) arising in any way out of the use of this software, even
#  if advised of the possibility of such damage.
#
#  $Id$
#

NAME = @PACKAGE_NAME@
VERSION = @PACKAGE_VERSION@
TARNAME = @PACKAGE_TARNAME@
PREFIX = @prefix@

BACK_END_PKGS = boost libdwarf libelf libmonitor libunwind libxml2  \
	lzma symtabAPI xed2

PACKAGES = $(BACK_END_PKGS)  \
	binutils old-monitor open-analysis oprofile perfmon xerces zlib

DIST_FILES = LICENSE Makefile.conf.in Makefile.in README*  \
	configure configure.ac externs.conf.in options.conf.in  \
	autogen build-utils config distfiles

NOBUILD_COOKIE = .dont-build
EXTERNS_FILE = externs.conf
FAILURE_FILE = BUILD-FAILURE

.PHONY: all install install-all back-end backend
.PHONY: fetch fetch-all clean clean-all
.PHONY: distclean distclean-all distclean-plus dist
.PHONY: pre-build post-build
.PHONY: $(PACKAGES)

all install install-all: pre-build $(PACKAGES)
	$(MAKE) post-build
	@echo "Externals build complete at `date`"

back-end backend: pre-build $(BACK_END_PKGS)
	$(MAKE) post-build
	@echo "Externals back-end build complete at `date`"

libdwarf: libelf zlib
libelf:   zlib
libunwind: lzma
oprofile:  binutils
symtabAPI: binutils boost libelf

#
# Catch package failures and save them in FAILURE_FILE.  This allows
# us to report the failure after a successful target.  Externals's
# top-level targets are entire package builds, so the output in a
# parallel build often hides the error from another package.
#
pre-build:
	@rm -f $(FAILURE_FILE)

post-build:
	@if test -f $(FAILURE_FILE) ; then \
		echo "BUILD FAILURE in the following packages:" ; \
		cat $(FAILURE_FILE) ; \
	fi
	@-if test ! -f "$(PREFIX)/$(EXTERNS_FILE)" ; then \
		echo "reinstall $(EXTERNS_FILE) ..." ;  \
		cp "$(EXTERNS_FILE)" "$(PREFIX)/$(EXTERNS_FILE)" ; \
	fi

$(PACKAGES): pre-build
	@if $(MAKE) -C $@ install ; then : ; else \
		echo $@ >>$(FAILURE_FILE) ; \
		echo "BUILD FAILURE:" $@ ;  \
		exit 2 ; \
	fi
	$(MAKE) post-build

fetch fetch-all:
	@for pkg in $(PACKAGES) ; do  \
		$(MAKE) -C $$pkg fetch || exit 2 ;  \
	done

clean clean-all:
	@for pkg in $(PACKAGES) ; do  \
		$(MAKE) -C $$pkg clean ;  \
	done

#
# We use distclean to undo everything in each package directory but
# leave the top-level configure intact, and use distclean-plus to
# also erase the top-level files.  The no-build cookies are part of
# the top-level configure.
#
# Also, externs.conf is part of the install, so never delete it.
#
distclean distclean-all:
	@for pkg in $(PACKAGES) ; do  \
		$(MAKE) -C $$pkg distclean ;  \
	done

distclean-plus: distclean-all
	@for pkg in $(PACKAGES) ; do  \
		rm -f "$$pkg/$(NOBUILD_COOKIE)" ;  \
	done
	rm -f config.log config.status options.conf
	rm -f Makefile.conf Makefile

#
# Make a tar file of the source tree.
#
MY_DIST_DIR = $(TARNAME)-$(VERSION)
MY_TAR_FILE = $(TARNAME)-$(VERSION).tar
SRCDIR = @srcdir@

dist:
	@echo "cleaning source tree (ignore errors) ..."
	-cd "$(SRCDIR)" && test -f Makefile && $(MAKE) clean
	rm -f "$(MY_DIST_DIR)" "$(MY_TAR_FILE)" "$(MY_TAR_FILE).gz"
	ln -s "$(SRCDIR)" "$(MY_DIST_DIR)"
	tar cf "$(MY_TAR_FILE)"  \
		--exclude .git   \
		--exclude .svn   \
		--exclude '.*-done'  \
		--exclude $(NOBUILD_COOKIE)  \
		$(addprefix $(MY_DIST_DIR)/,$(PACKAGES))  \
		$(addprefix $(MY_DIST_DIR)/,$(DIST_FILES))
	rm -f "$(MY_DIST_DIR)"
	gzip "$(MY_TAR_FILE)"
	@ls -l "$(MY_TAR_FILE).gz"

